Msg( "Loaded HGF Client Effects File.\n" )

HG.Render = {
	hats = {}, auras = {}
}

// Aura code below

local auras = {}

local function RemoveAura( pl )
	pl:StopParticles()
	pl.Aura = nil
end

local function CreateAura( pl, auraid )

	if pl.Aura then RemoveAura( pl ) end

	local oldParticleEffectAttach = ParticleEffectAttach( auraid, PATTACH_ABSORIGIN_FOLLOW, pl, 1 )
	
	pl.Aura = auraid

end


usermessage.Hook( "hgf_EquipAura", function( um )
	local pl = um:ReadEntity()
	local auraid = um:ReadString()
	CreateAura( pl, auraid )
end )

local function CleanAuras( )
	local k, v
	
	for k, v in ipairs( player.GetAll() ) do
		if not ValidEntity( v ) then
			RemoveAura( v )
		elseif v.Aura == "" then
			RemoveAura( v )
		end
	end
end
hook.Add( "Think", "hgf_ClientAuraThink", CleanAuras )

// Hat code below

local function CreateHat( pl, itemid, pos, ang, scale, color )

	local model = HG.Item(itemid).Model
	local oldpos, oldang = pos, ang

	SafeRemoveEntity(pl.Hat)
	
	local hat
	
	hat = ClientsideModel( model, RENDERGROUP_OPAQUE )
	hat:SetParent( pl )
	hat:DrawShadow( false )
	hat:SetNoDraw( true )
	hat:SetPos( pos )
	hat:SetAngles( ang )
	hat.OldPos = oldpos
	hat.OldAng = oldang
	hat.Scale = scale
	hat.Color = color
	hat.ItemID = itemid
	
	pl.Hat = hat
	hat.Owner = pl
	
	table.insert(HG.Render.hats, hat)
	
	PrintTable(HG.Render.hats)
	
end

usermessage.Hook( "hgf_EquipHat", function( um )
	
	local pl = um:ReadEntity()
	local itemid = um:ReadString()
	
	if itemid == "" then
		HG.Render.hats[k] = nil
	else
		local pos = HG.Item(itemid).Pos
		local ang = HG.Item(itemid).Ang
		local scale = HG.Item(itemid).Scale or Vector(1,1,1)
		local color = HG.Item(itemid).Color or Color(255,255,255,255)
		
		CreateHat( pl, itemid, pos, ang, scale, color )
	end
end )

local function CleanHats( )
	local k, v
	
	for k, v in ipairs( player.GetAll() ) do
		if not ValidEntity( v ) then
			HG.Render.hats[k] = nil
		elseif not ValidEntity( v.Owner ) then
			SafeRemoveEntity( v )
			HG.Render.hats[k] = nil
		end
	end
end

/*
	local e_pos, e_ang = Vector(CONV_HE_X:GetInt(),CONV_HE_Y:GetInt(),CONV_HE_Z:GetInt()), Angle(CONV_HE_X:GetInt(),CONV_HE_Y:GetInt(),CONV_HE_Z:GetInt())
	local e_sca = Vector(CONV_HE_SX:GetInt(),CONV_HE_SY:GetInt(),CONV_HE_SZ:GetInt())
		// Hat editor
	pos.x = pos.x + e_pos.x
	pos.y = pos.y + e_pos.y
	pos.z = pos.z + e_pos.z
	
	ang:RotateAroundAxis( right, e_ang.p )
	ang:RotateAroundAxis( up, e_ang.y )
	ang:RotateAroundAxis( forward, e_ang.r )
	
	if scale then
		scale.x = scale.x + e_sca.x
		scale.y = scale.y + e_sca.y
		scale.z = scale.z + e_sca.z
	end
*/

local function DrawHats()

	if GetConVar( "hg_drawhats" ):GetBool() then

		local k, v, pos, ang
		
		CleanHats( )
		
		for k, v in ipairs( player.GetAll() ) do
			
			if not v:Alive() and ValidEntity( v.Hat ) then
				
				v.Hat:SetNoDraw(true)
				v = v:GetRagdollEntity()
			
			elseif v:Alive() and ValidEntity( v.Hat ) then
				
				if GetViewEntity() == LocalPlayer() and v:EntIndex() == LocalPlayer():EntIndex() then
					--print( "Player is in First Person and should not render his own hat" )
					return
				end
				
				local oldpos  = v.Hat.OldPos
				local oldang = v.Hat.OldAng
				local scale = v.Hat.Scale
				local color = v.Hat.Color
				local matrix = v:GetBoneMatrix(v:LookupBone("ValveBiped.Bip01_Head1"))
				
				oldpos = HG.ITEMS[v.Hat.ItemID].Pos
				oldang = HG.ITEMS[v.Hat.ItemID].Ang
				
				if matrix then
					local angle = matrix:GetAngle()
					local position = matrix:GetTranslation()
					
					// Modify angle
					ang = angle
					local forward, right, up = ang:Forward(), ang:Right(), ang:Up()
				
					ang:RotateAroundAxis( right, -90 )
					ang:RotateAroundAxis( forward, 90 )
					
					ang:RotateAroundAxis( ang:Right(), oldang.p )
					ang:RotateAroundAxis( ang:Up(), oldang.y )
					ang:RotateAroundAxis( ang:Forward(), oldang.r )

					// Modify position
					pos = position
					pos:Add(ang:Right() * oldpos.x)
					pos:Add(ang:Forward() * oldpos.z)
					pos:Add(ang:Up() * oldpos.y)

					// Update ang pos and scale
					v.Hat:SetRenderOrigin(pos)
					v.Hat:SetRenderAngles(ang)

					if scale then
						v.Hat:SetModelScale(scale)
					end

					v.Hat:DrawModel( )
					v.Hat:SetColor(color or Color(0,0,0,255))

				end
				
			end
		end
	
	end
	
end
hook.Add( "PostDrawOpaqueRenderables", "DrawHats", DrawHats )
